package com.dtt.company.score.service;

import java.util.List;

import com.dtt.company.score.dao.ScoreDao;
import com.dtt.company.score.model.ScoreModel;
import com.dtt.company.util.FmtEmpty;

public class ScoreService {
	private ScoreDao dao=new ScoreDao();

	public String insert(ScoreModel model) {
		//添加的逻辑主键（账号）存在时，不允许添加
		ScoreModel mdb=selectModel(model);
		if(mdb==null) { 
			Integer result=dao.insert(model);
			return String.valueOf(result);
		} 
		return "E";
	}

	public String delete(ScoreModel model) {
		Integer result=dao.delete(model);
		return String.valueOf(result);
	}

	public String update(ScoreModel model) {
		Integer result=dao.update(model);
		return String.valueOf(result);
	}

	/**
	 *多条件模糊查询出一些记录
	 * @author Dtt
	 * @return
	 */
	public List<ScoreModel> select(ScoreModel model) {
		String empCode=model.getEmpCode();
//		System.out.println("code==="+code);
		if(empCode!=null)
			model.setEmpCode("%"+empCode+"%") ;
		String proCode=model.getProCode();
//		System.out.println("name==="+name);
		if(proCode!=null)
			model.setProCode("%"+proCode+"%");
		return dao.select(model);
	}

	/**
	 * 根据逻辑主键查询出唯一的记录，主键不存在返回null
	 * @author Dtt
	 * @return
	 */
	public ScoreModel selectModel(ScoreModel model) {
		ScoreModel temp=new ScoreModel(model.getEmpCode(),model.getProCode());
		List<ScoreModel> list = dao.select(temp);
		if(FmtEmpty.isEmpty(list)) {
			return null;
		}
		return list.get(0);
		
	}
	//分页：根据多条件的模糊查询的数据总数
	public int selectCount(ScoreModel model) {
		ScoreModel mdb=new ScoreModel();
		String empCode=model.getEmpCode();
		mdb.setEmpCode(empCode == null ? "%%":"%"+empCode+"%");
		String proCode=model.getProCode();
		mdb.setProCode(proCode == null ? "%%":"%"+proCode+"%");
		mdb.setPageOn(false);
		return dao.selectCount(mdb);
		
	}
}
